/**
 iTextUploader
 (C) 2008 MyselftDev (myselftdev@gmail.com)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* Revision: $Id: PreferencesView.m 118 2009-04-27 19:22:56Z myselftdev $ */

#import "PreferencesView.h"

@implementation PreferencesView

@synthesize mainView, home, preferencesTable;

- (id)initWithFrame:(CGRect)frame
		 dataObject:(PreferencesData*)theDataObject
	 homeViewObject:(HomeView*)theHomeViewObject
	 mainViewObject:(MainView*)theMainViewObject
{
	if ((self = [super initWithFrame:frame]))
	{
		data = [theDataObject retain];
		home = [theHomeViewObject retain];
		mainView = [theMainViewObject retain];
		
		mainView.navItem.title = @"Preferences";
		
		self.backgroundColor = HEXCOLOR(0xDAE1EBFF);
		
		tableController = [[PreferencesTableController alloc] initWithData:theDataObject parent:self];
		preferencesTable = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
		preferencesTable.dataSource = tableController;
		preferencesTable.delegate = tableController;
		preferencesTable.backgroundColor = HEXCOLOR(0xDAE1EBFF);
		[self addSubview:preferencesTable];
		
		// The "About" page
		aboutView = [ [UIWebView alloc] initWithFrame:frame];
		aboutView.delegate = self;
		aboutView.detectsPhoneNumbers = NO;
		NSURLRequest *aRequest=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"AboutText" ofType:@"html"]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
		[aboutView loadRequest:aRequest];
	}
	return self;
}

- (void)dealloc
{
	[preferencesTable release];
	[tableController release];
	
	[aboutView release];
	
	[data release];
	[home release];
	[mainView release];
	
	[super dealloc];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	[aboutView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById(\"title\").innerHTML = 'iTextUploader %@'", @APPVERSION]];
}

-(void)isGoingToVisible
{
	mainView.navItem.title = @"Preferences";
	tableController.level = kLevelMain;
	UIBarButtonItem *aboutButton = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStylePlain target:self action:@selector(showAbout)];
	[aboutButton autorelease];
	[mainView.navItem setRightBarButtonItem:aboutButton animated:NO];
	[aboutView removeFromSuperview];
	mainView.navBar.delegate = self;
}

-(void)isGoingToHidden
{
	[mainView.navItem setRightBarButtonItem:nil animated:NO];
	if(tableController.level == kLevelIntervals)
	{
		[mainView.navBar popNavigationItemAnimated:NO];
	}
	if(tableController.level == kLevelAdvanced)
	{
		[mainView.navBar popNavigationItemAnimated:NO];
	}
	mainView.navBar.delegate = nil;
}

-(void)showAbout
{
	[self addSubview:aboutView];
	mainView.navItem.title = @"About";
	
	UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(hideAbout)];
	[backButton autorelease];
	[mainView.navItem setRightBarButtonItem:backButton animated:NO];
}

-(void)hideAbout
{
	[aboutView removeFromSuperview];
	[self isGoingToVisible];
}

-(void)donateButtonClicked
{
	NSString *URLString = [NSString stringWithFormat:@"%@%@", @BASEURL, @"donate.php"];
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URLString]];
}

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
	static UINavigationItem	*last = nil;
	
	if(!last)
	{
		last = item;
		[navigationBar popNavigationItemAnimated:NO];
		return NO;
	}
	
	if(tableController.level == kLevelAdvanced)
	{
		tableController.level = kLevelMain;
	}
	else
	{
		tableController.level = kLevelAdvanced;
	}
	[preferencesTable reloadData];
	[tableController automaticUploadSwitchChanged];
	last = nil;
	
	NSIndexPath *selected = [preferencesTable indexPathForSelectedRow];
	if(selected)
	{
		[preferencesTable deselectRowAtIndexPath:selected animated:NO];
	}
	
	return YES;
}

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item
{
	if(tableController.level == kLevelMain)
	{
		tableController.level = kLevelAdvanced;
	}
	else
	{
		tableController.level = kLevelIntervals;
	}
	[preferencesTable reloadData];
	[tableController automaticUploadSwitchChanged];
	
	NSIndexPath *selected = [preferencesTable indexPathForSelectedRow];
	if(selected)
	{
		[preferencesTable deselectRowAtIndexPath:selected animated:NO];
	}
	
	return YES;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
	NSURL *theURL = [request URL];
	NSURL *ourURL = [NSURL URLWithString:@BASEURL];
	
	if([@"file" isEqualToString:[theURL scheme]] || [@"about" isEqualToString:[theURL scheme]] ||
	   [[ourURL host] isEqualToString:[theURL host]])
	{
		return YES;
	}
	
	[[UIApplication sharedApplication] openURL:theURL];
	return NO;
}

- (void) enableAutomaticUpload:(BOOL)value
{
#if (TARGET_IPHONE_SIMULATOR)
	NSLog(@"Here I should %s automatic upload", (value ? "Enable" : "Disable"));
#else
	NSLog(@"Enabling automatic upload");
	
	if(value)
	{
		system("/Applications/iTextUploader.app/service load");
	}
	else
	{
		system("/Applications/iTextUploader.app/service unload");
	}
#endif
}

@end